# encoding: utf-8
class AdvertiseRule < ActiveRecord::Base
  has_paper_trail :version => :paper_trail_version, :versions => :paper_trail_versions
  belongs_to :advertise_ruleable, :polymorphic => true 
  belongs_to :advertise_group

  after_create :remove_conflict_advertise_rule

  private

  def remove_conflict_advertise_rule
    AdvertiseRule.where(advertise_ruleable_type: self.advertise_ruleable_type,
      advertise_ruleable_id: self.advertise_ruleable_id,
      ).where("advertise_group_id != ?",self.advertise_group_id).destroy_all
  end

end
